package com.c7.servlet;

import com.c7.bean.NewsBean;
import com.c7.dan.NewsDao;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * 1.接收post网址 /newsAdd.jsp
 * 2.接收到到传递过来的新闻参数，新闻标题，新闻内容
 * 3.完成新闻的添加操作
 * 4.跳转到其他页面
 */
@WebServlet("/newsAdd.jsp")
public class NewsAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String newsTitle = req.getParameter("NewsTitle");
        String newsContent = req.getParameter("NewsContent");
        NewsBean newsBean = new NewsBean();
        newsBean.setNewsTitle(newsTitle);
        newsBean.setNewsContent(newsContent);
        NewsDao newsDao = new NewsDao();
        newsDao.add(newsBean);
        // 在Servlet中调用使用网址跳转 请求结果对象
        resp.sendRedirect("news.jsp");//参数是相对网址
    }
}
